<template>
  <div>
    <el-col :span="4">
      <el-input v-model="patientName" placeholder="请输入患者姓名"></el-input>
    </el-col>
    <el-col :span="4">
      <el-input v-model="patientCard" placeholder="请输入患者证件"></el-input>
    </el-col>
    <el-button type="info" @click="search">查询</el-button>

    <el-dialog title="患者信息" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="患者姓名" :label-width="formLabelWidth">
          <el-input v-model="form.patientName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="患者年龄" :label-width="formLabelWidth">
          <el-input v-model="form.patientAge" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="患者电话" :label-width="formLabelWidth">
          <el-input v-model="form.patientPhone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="患者证件" :label-width="formLabelWidth">
          <el-input v-model="form.patientCard" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitform">确 定</el-button>
      </div>
    </el-dialog>

    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="patientId" label="患者编号" width="180">
      </el-table-column>
      <el-table-column prop="patientName" label="患者姓名" width="180">
      </el-table-column>
      <el-table-column prop="patientAge" label="患者年龄" width="100">
      </el-table-column>
      <el-table-column prop="patientPhone" label="患者电话" width="180">
      </el-table-column>
      <el-table-column prop="patientCard" label="患者证件" width="180">
      </el-table-column>
      <el-table-column prop="patientSalary" label="余额" width="180">
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="resetPWD(scope.row)"
            >重置密码</el-button>
        </template>
      </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="0"
            :inactive-value="1"
            @change="change(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="100"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import {
  getlist,
  dele,
  update,
  search,
  change,
  resetpwd
} from "@/api/patient/patient.js";
export default {
  data() {
    return {
      tableData: [],
      currentPage: 1,
      total: 0,
      currentPage: 1,
      pageSize: 5,
      value: true,
      dialogFormVisible: false,
      form: {},
      formLabelWidth: "120px",
      patientName: "",
      patientCard: "",
    };
  },
  methods: {
    resetPWD(row){
      resetpwd(row.patientId).then(r=>{
        if(r.code == 200){
          this.$message.success("密码重置成功")
        }
      })
    },
    change(row) {
      dele({ patientId: row.patientId, status: row.status }).then((r) => {});
    },
    search() {
      search({
        patientName: this.patientName,
        patientCard: this.patientCard,
        pageNumber: this.currentPage,
        pageSize: this.pageSize,
      }).then((r) => {
        this.tableData = r.data.list;
        this.total = r.data.total;
      });
    },
    getlist() {
      getlist({ pageNumber: this.currentPage, pageSize: this.pageSize }).then(
        (r) => {
          this.tableData = r.data.list;
          this.total = r.data.total;
        }
      );
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.getlist();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this. currentPage = val;
      this.getlist();
    },
    update(row) {
      this.form = JSON.parse(JSON.stringify(row));
      this.dialogFormVisible = true;
    },
    submitform() {
      update(this.form).then((r) => {
        if (r.code == 200) {
          this.getlist();
          this.$message.success("修改成功");
          this.dialogFormVisible = false;
        } else {
          this.$message.error("修改失败");
        }
      });
    },
  },
  mounted() {
    this.getlist();
  },
};
</script>

<style>
</style>